package com.hknc.btscale.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hknc.btscale.R;
import com.hknc.btscale.bean.Test;

import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created by guiyongsun on 2022/2/14 0:45.
 *
 * @Description:
 */
public class TableAdapter extends RecyclerView.Adapter<TableAdapter.MyViewHolder> {
    private CopyOnWriteArrayList<Test> list;
    private Context mContext;

    public TableAdapter(CopyOnWriteArrayList<Test> list, Context context) {
        this.list = list;
        mContext = context;
    }

    public void setList(CopyOnWriteArrayList<Test> list){
        this.list=list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        MyViewHolder viewHolder = new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_table_content, parent, false));
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.nameTv.setText(list.get(position).getName());
        holder.countTv.setText(list.get(position).getCount() + "");
        holder.priceTv.setText(list.get(position).getPrice() + "");
        holder.contentPriceTv.setText(list.get(position).getCountPrice() + "");
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView nameTv, countTv, priceTv, contentPriceTv;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            nameTv = itemView.findViewById(R.id.name_tv);
            countTv = itemView.findViewById(R.id.num_tv);
            priceTv = itemView.findViewById(R.id.price_tv);
            contentPriceTv = itemView.findViewById(R.id.sum_tv);
        }
    }
}
